package pl.edu.agh.uddiProxy.client.admin;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class AdminPanel extends HorizontalPanel implements EntryPoint {

	private static final int VISIBLE_ITEMS = 10;
	
	private ListBox userListBox = new ListBox();
	private UserDetailsGrid detailsGrid;
	
	public AdminPanel() {
		setSpacing(10);
		
		detailsGrid = new UserDetailsGrid() {
			@Override
			protected void update() {
				fillUserList();
			}
		};
		
		userListBox.setVisibleItemCount(VISIBLE_ITEMS);
		userListBox.setWidth("100%");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(userListBox);
		
		Button addButton = new Button("Add");
		Button deleteButton = new Button("Delete");
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(addButton);
		horizontalPanel.add(deleteButton);
		horizontalPanel.setSpacing(4);
		verticalPanel.add(horizontalPanel);
		
		add(verticalPanel);
		add(detailsGrid);
		
		fillUserList();
		
		addButton.addClickListener(new ClickListener() {
		
			public void onClick(Widget arg0) {
				detailsGrid.clear();
			}
		});
		
		deleteButton.addClickListener(new ClickListener() {
		
			public void onClick(Widget arg0) {
				String username = userListBox.getItemText(userListBox.getSelectedIndex());
				if (!username.equalsIgnoreCase("admin")) {
					UserService.Util.getInstance().deleteUser(username, new AsyncCallback<Void>() {
					
						public void onSuccess(Void v) {
							detailsGrid.clear();
							fillUserList();
						}
					
						public void onFailure(Throwable throwable) {
							Window.alert(throwable.getMessage());
						}
					});
				}
			}
		});
		
		userListBox.addChangeListener(new ChangeListener() {
		
			public void onChange(Widget widget) {
				String username = userListBox.getItemText(userListBox.getSelectedIndex());
				UserService.Util.getInstance().getUserDetails(username, new AsyncCallback<User>() {
				
					public void onSuccess(User user) {
						detailsGrid.setUser(user);
					}
				
					public void onFailure(Throwable throwable) {
						Window.alert(throwable.getMessage());
					}
				});
			}
		});
	}

	private void fillUserList() {
		UserService.Util.getInstance().getUsers(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable throwable) {
				Window.alert(throwable.getMessage());
			}

			public void onSuccess(List<String> list) {
				userListBox.clear();
				
				for (String user : list) {
					userListBox.addItem(user);
				}
			}
		});		
	}

	public void onModuleLoad() {
		RootPanel.get("panel").add(this);
	}

}
